package com.bw.module_live.widget

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.TypeEvaluator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.PointF
import android.util.AttributeSet
import android.view.MotionEvent
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.core.animation.addListener
import com.bw.module_live.R
import java.util.Random

/**
 * @author ytx
 * @date 2025-10-28 14:05
 * @description
 */
class BezierView:FrameLayout {
    constructor(context: Context):this(context, null)
    constructor(context: Context,attr:AttributeSet?):this(context,attr,0)
    constructor(context: Context,attr:AttributeSet?,defStyleAttr:Int):this(context,attr,defStyleAttr,0)
    constructor(context: Context,attr:AttributeSet?,defStyleAttr:Int,defStyleRes:Int):super(context,attr,defStyleAttr,defStyleRes){

    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (event?.action == MotionEvent.ACTION_DOWN){
            addImageView()
        }
        return true
    }

    
    private fun addImageView() {
        //1:创建imageview并添加
        val imageView = ImageView(context)
        imageView.setImageResource(R.drawable.ic_love)
        imageView.layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
        addView(imageView)
        //2：贝塞尔曲线完成动画
        val  startPointF = PointF(measuredWidth/2f,measuredHeight.toFloat())
        //宽度的随机数
        val random = Random()
        val point1 = PointF(random.nextInt(measuredWidth).toFloat(),measuredHeight/3f)
        val point2 = PointF(random.nextInt(measuredWidth).toFloat(),measuredHeight * 2f/3f)
        val endPointF = PointF(measuredWidth/2f,0f)
        //透明度
        val alphaAnimator = ObjectAnimator.ofFloat(imageView,"alpha",1f,0f)
        //位置
        val valueAnimator = ValueAnimator.ofObject(BezierEvaluator(point1, point2), startPointF, endPointF)
        valueAnimator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener{
            override fun onAnimationUpdate(animation: ValueAnimator) {
                val pointF = animation.getAnimatedValue() as PointF
                //设置图片的位置
                imageView.x = pointF.x
                imageView.y = pointF.y
            }
        })
        val animatorSet = AnimatorSet()
        animatorSet.playTogether(alphaAnimator,valueAnimator)



        animatorSet.addListener(object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {

            }

            override fun onAnimationEnd(animation: Animator) {
               removeView(imageView) //防止占用内存
                if (callBack != null){
                    callBack?.onEnd()
                }
            }

            override fun onAnimationCancel(animation: Animator) {

            }

            override fun onAnimationRepeat(animation: Animator) {

            }
        })


        animatorSet.setDuration(2000)
        animatorSet.start()
    }

    /**
     * 估值器：按照贝塞尔曲线公式计算值
     * 三阶：四个点(起点 终点 2个控制点)
     * (1-t)³P₀ + 3(1-t)²tP₁ + 3(1-t)t²P₂ + t³P₃
     * mPointF1:控制点1
     * mPointF2:控制点2
     */
    inner class BezierEvaluator(val mPointF1:PointF,val mPointF2:PointF):TypeEvaluator<PointF>{

        /**
         * fraction:0-1
         * startValue：起点
         * endValue：终点
         */
        override fun evaluate(fraction: Float, startValue: PointF, endValue: PointF): PointF {
            val pointF = PointF()
            pointF.x = (1-fraction)* (1-fraction)* (1-fraction)*startValue.x +
                        3 * (1-fraction) * (1-fraction) * fraction * mPointF1.x +
                        3 * (1-fraction) * fraction * fraction * mPointF2.x +
                        fraction * fraction * fraction * endValue.x
            pointF.y = (1-fraction)* (1-fraction)* (1-fraction)*startValue.y +
                        3 * (1-fraction) * (1-fraction) * fraction * mPointF1.y +
                        3 * (1-fraction) * fraction * fraction * mPointF2.y +
                        fraction * fraction * fraction * endValue.y
            return pointF
        }

    }
    private var callBack:CallBack? = null
    fun setCallBack(callBack: CallBack){
        this.callBack = callBack
    }

    interface CallBack{
        fun onEnd()
    }
}